<script>
import { STATES, STATES_ENUM } from '@shell/plugins/dashboard-store/resource-class';
import FleetStatus from '@shell/components/fleet/FleetStatus';

const getResourceDefaultState = (labelGetter, stateKey) => {
  return {
    ready: {
      count:  0,
      color:  STATES[STATES_ENUM.READY].color,
      label:  labelGetter(`${ stateKey }.${ STATES_ENUM.READY }`, null, STATES[STATES_ENUM.READY].label ),
      status: STATES_ENUM.READY
    },
    info: {
      count:  0,
      color:  STATES[STATES_ENUM.INFO].color,
      label:  labelGetter(`${ stateKey }.${ STATES_ENUM.INFO }`, null, STATES[STATES_ENUM.INFO].label ),
      status: STATES_ENUM.INFO
    },
    warning: {
      count:  0,
      color:  STATES[STATES_ENUM.WARNING].color,
      label:  labelGetter(`${ stateKey }.${ STATES_ENUM.WARNING }`, null, STATES[STATES_ENUM.WARNING].label ),
      status: STATES_ENUM.WARNING
    },
    notready: {
      count:  0,
      color:  STATES[STATES_ENUM.NOT_READY].color,
      label:  labelGetter(`${ stateKey }.${ STATES_ENUM.NOT_READY }`, null, STATES[STATES_ENUM.NOT_READY].label ),
      status: STATES_ENUM.NOT_READY
    },
    error: {
      count:  0,
      color:  STATES[STATES_ENUM.ERROR].color,
      label:  labelGetter(`${ stateKey }.${ STATES_ENUM.ERROR }`, null, STATES[STATES_ENUM.ERROR].label ),
      status: STATES_ENUM.ERROR
    },
    errapplied: {
      count:  0,
      color:  STATES[STATES_ENUM.ERR_APPLIED].color,
      label:  labelGetter(`${ stateKey }.${ STATES_ENUM.ERR_APPLIED }`, null, STATES[STATES_ENUM.ERR_APPLIED].label ),
      status: STATES_ENUM.ERR_APPLIED,
    },
    waitapplied: {
      count:  0,
      color:  STATES[STATES_ENUM.WAIT_APPLIED].color,
      label:  labelGetter(`${ stateKey }.${ STATES_ENUM.WAIT_APPLIED }`, null, STATES[STATES_ENUM.WAIT_APPLIED].label ),
      status: STATES_ENUM.WAIT_APPLIED
    },
    unknown: {
      count:  0,
      color:  STATES[STATES_ENUM.UNKNOWN].color,
      label:  labelGetter(`${ stateKey }.${ STATES_ENUM.UNKNOWN }`, null, STATES[STATES_ENUM.UNKNOWN].label ),
      status: STATES_ENUM.UNKNOWN
    }
  };
};

export default {

  name: 'FleetSummary',

  components: { FleetStatus },

  props: {
    bundles: {
      type:    Array,
      default: () => [],
    },
    value: {
      type:     Object,
      required: true,
    },

    stateKey: {
      type:    String,
      default: 'fleet.fleetSummary.state'
    },
  },

  computed: {

    repoName() {
      return this.value.metadata.name;
    },

    bundleCounts() {
      const resources = this.bundles.filter((item) => item.repoName === this.repoName);

      if (!resources.length) {
        return [];
      }

      const out = { ...getResourceDefaultState(this.$store.getters['i18n/withFallback'], this.stateKey) };

      resources.forEach(({ status, metadata }) => {
        if (!status) {
          out[STATES_ENUM.UNKNOWN].count += 1;

          return;
        }

        const k = status?.summary.ready > 0 && status?.summary.desiredReady === status.summary.ready;

        if (k) {
          out.ready.count += 1;

          return;
        }

        const state = metadata.state?.name?.toLowerCase();

        if (state && out[state]) {
          out[state].count += 1;

          return;
        }

        const { conditions } = status;

        const notReady = conditions.find(({ transitioning, message }) => {
          return transitioning && !message.includes(STATES_ENUM.ERROR) && !message.toLowerCase().includes(STATES_ENUM.ERR_APPLIED);
        });

        if (!!notReady) {
          out.notready.count += 1;

          return;
        }

        // check conditions
        const errApplied = conditions.find(({ error, message }) => !!error && message.toLowerCase().includes(STATES_ENUM.ERR_APPLIED));

        if (errApplied) {
          out[STATES_ENUM.ERR_APPLIED].count += 1;

          return;
        }

        const errorState = conditions.find(({ error, message }) => !!error && message.toLowerCase().includes(STATES_ENUM.ERROR));

        if (out[errorState]) {
          out[errorState].count += 1;

          return;
        }

        out.unknown.count += 1;
      });

      return Object.values(out).map((item) => {
        item.value = item.count;

        return item;
      });
    },

    resourceCounts() {
      const resources = this.value.status.resources || [];
      const out = { ...getResourceDefaultState(this.$store.getters['i18n/withFallback'], this.stateKey) };

      resources.forEach(({ state }) => {
        const k = state?.toLowerCase();

        if (out[k]) {
          out[k].count += 1;

          return;
        }
        out.unknown.count += 1;
      });

      return Object.values(out).map((item) => {
        item.value = item.count;

        return item;
      });
    },

  },

};
</script>

<template>
  <div class="row flexwrap">
    <FleetStatus
      v-if="bundleCounts.length"
      title="Bundles"
      :values="bundleCounts"
      value-key="count"
      data-testid="gitrepo-bundle-summary"
    />
    <FleetStatus
      title="Resources"
      :values="resourceCounts"
      value-key="count"
      data-testid="gitrepo-deployment-summary"
    />
  </div>
</template>
<style lang="scss" scoped>
   .flexwrap .fleet-status {
    max-width: 50%;
    margin-right: 15px;

    &:last-child {
      margin: 0
    }
  }
  .countbox {
    min-width: 150px;
    width: 12.5%;
    margin-bottom: 10px;
  }
</style>
